using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qVKKOPF = from m in mtemp.VKKOPF select m;
       DBMapper.MapVKKOPF(qVKKOPF.ToList(), true);
       */
        public List<VKKOPF> MapVKKOPF(XElement item2, bool mergedb)
        {
            List<VKKOPF> result = new List<VKKOPF>();
            var item4 = from item3 in item2.Elements()
                        select new VKKOPF
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            VKKMANDANT = (string)item3.Element("VKKMANDANT"),
                            VKKAUFKRS = (string)item3.Element("VKKAUFKRS"),
                            VKKAUFLFD = (string)item3.Element("VKKAUFLFD"),
                            VKKNEUDAT = (string)item3.Element("VKKNEUDAT"),
                            VKKKUNDNR = (string)item3.Element("VKKKUNDNR"),
                            VKKSACHBE = (string)item3.Element("VKKSACHBE"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapVKKOPF(result, mergedb);
          return result;
        }
        ///DBMapper.MapVKKOPF(mtemp.VKKOPF.ToList<VKKOPF>(), true);        
       public void MapVKKOPF(List<VKKOPF> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from VKKOPF row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.VKKOPF.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.VKKOPF.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.VKKOPF.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "VKKOPF")]
    public class VKKOPF : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _VKKMANDANT="0";
        [Column]
        public string VKKMANDANT
        {
            get
            {
                return _VKKMANDANT;
            }
            set
            {
                if (value != _VKKMANDANT)
                {
                     _VKKMANDANT = value;
                    NotifyPropertyChanged("VKKMANDANT");
                }
            }
        }
        private string  _VKKAUFKRS="0";
        [Column]
        public string VKKAUFKRS
        {
            get
            {
                return _VKKAUFKRS;
            }
            set
            {
                if (value != _VKKAUFKRS)
                {
                     _VKKAUFKRS = value;
                    NotifyPropertyChanged("VKKAUFKRS");
                }
            }
        }
        private string  _VKKAUFLFD="0";
        [Column]
        public string VKKAUFLFD
        {
            get
            {
                return _VKKAUFLFD;
            }
            set
            {
                if (value != _VKKAUFLFD)
                {
                     _VKKAUFLFD = value;
                    NotifyPropertyChanged("VKKAUFLFD");
                }
            }
        }
        private string  _VKKNEUDAT="0";
        [Column]
        public string VKKNEUDAT
        {
            get
            {
                return _VKKNEUDAT;
            }
            set
            {
                if (value != _VKKNEUDAT)
                {
                     _VKKNEUDAT = value;
                    NotifyPropertyChanged("VKKNEUDAT");
                }
            }
        }
        private string  _VKKKUNDNR="0";
        [Column]
        public string VKKKUNDNR
        {
            get
            {
                return _VKKKUNDNR;
            }
            set
            {
                if (value != _VKKKUNDNR)
                {
                     _VKKKUNDNR = value;
                    NotifyPropertyChanged("VKKKUNDNR");
                }
            }
        }
        private string  _VKKSACHBE="0";
        [Column]
        public string VKKSACHBE
        {
            get
            {
                return _VKKSACHBE;
            }
            set
            {
                if (value != _VKKSACHBE)
                {
                     _VKKSACHBE = value;
                    NotifyPropertyChanged("VKKSACHBE");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<VKKOPF> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\VKKOPF\\VKKOPF" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\VKKOPF" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("VKKOPF");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (VKKOPF row in ListInput)
                {
                    XElement f = new XElement("VKKOPF");
                    XElement MAINKEY = new XElement("MAINKEY");
                     f.Add(MAINKEY);
                   
                        XElement VKKMANDANT = new XElement("VKKMANDANT");
                         VKKMANDANT.Value = row.VKKMANDANT;
                          f.Add(VKKMANDANT);
                        XElement VKKAUFKRS = new XElement("VKKAUFKRS");
                         VKKAUFKRS.Value = row.VKKAUFKRS;
                          f.Add(VKKAUFKRS);
                        XElement VKKAUFLFD = new XElement("VKKAUFLFD");
                         VKKAUFLFD.Value = row.VKKAUFLFD;
                          f.Add(VKKAUFLFD);
                        XElement VKKNEUDAT = new XElement("VKKNEUDAT");
                         VKKNEUDAT.Value = row.VKKNEUDAT;
                          f.Add(VKKNEUDAT);
                        XElement VKKKUNDNR = new XElement("VKKKUNDNR");
                         VKKKUNDNR.Value = row.VKKKUNDNR;
                          f.Add(VKKKUNDNR);
                        XElement VKKSACHBE = new XElement("VKKSACHBE");
                         VKKSACHBE.Value = row.VKKSACHBE;
                          f.Add(VKKSACHBE);
                  root.Add(f);
                }
                sw.Write(xdoc);
                sw.Close();
                sw.Dispose();
            }
            catch (Exception ex)
            { 
            }
      
        }                                        
    }
}
